package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.baseinfo.Airport;
import com.tmc.util.ObjectUtil;

@Repository
public class AirportDaoImpl extends BaseDaoImpl<Airport> {

	public Pagination<Airport> findAirportsByCon(ContextInfo context, Airport cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Airport c where c.enabledFlag = 'Y' ");
		if (ObjectUtil.hasText(cond.getAirportCode())) {
			sql.append(" and (c.airportCode like :airportCode or c.airportName like :airportCode or c.cityName like :airportCode or c.cityCode like :airportCode) ");
			params.put("airportCode", "%" + cond.getAirportCode() + "%");
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}

	public List<Airport> findAll() {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Airport c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}
	
	public List<Airport> findByCityCode(ContextInfo context, String cityCode) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Airport c where c.enabledFlag = 'Y' ");
		sql.append(" and c.cityCode = :cityCode ");
		params.put("cityCode", cityCode);
		return this.findList(sql.toString(), params);
	}

	public Airport getByCode(ContextInfo context, String airportCode) {
		if (!ObjectUtil.hasText(airportCode)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from Airport c where c.enabledFlag = 'Y' ");
		sql.append(" and c.airportCode = :airportCode ");
		params.put("airportCode", airportCode);
		return this.get(sql.toString(), params);
	}

}
